{% extends "base.html" %}

{% block title %}区域管理 - AI视频分析系统{% endblock %}

{% block content %}
<div class="container py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1"><i class="bi bi-map me-2"></i>区域管理</h2>
            <p class="text-muted mb-0">管理系统区域信息</p>
        </div>
        <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addAreaModal">
            <i class="bi bi-plus-circle me-1"></i> 添加新区域
        </button>
    </div>

    <!-- 操作状态提示 -->
    {% if error %}
    <div class="alert alert-danger alert-dismissible fade show mb-4">
        <i class="bi bi-exclamation-circle me-2"></i> {{ error }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    {% endif %}
    
    {% if success %}
    <div class="alert alert-success alert-dismissible fade show mb-4">
        <i class="bi bi-check-circle me-2"></i> {{ success }}
        <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
    </div>
    {% endif %}
    
    <!-- 搜索和过滤 -->
    <div class="card shadow-sm mb-4">
        <div class="card-body">
            <form class="row g-3">
                <div class="col-md-8">
                    <div class="input-group">
                        <span class="input-group-text"><i class="bi bi-search"></i></span>
                        <input type="text" class="form-control" placeholder="搜索区域名称..." 
                               name="search" value="{{ search or '' }}">
                    </div>
                </div>
                <div class="col-md-4">
                    <button type="submit" class="btn btn-primary w-100">
                        <i class="bi bi-search me-1"></i> 搜索
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- 区域信息表格 -->
    <div class="card shadow-sm">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover align-middle mb-0">
                    <thead class="table-light">
                        <tr>
                            <th>序号</th>
                            <th>区域名称</th>
                            <th>纬度</th>
                            <th>经度</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% if areas|length == 0 %}
                        <tr>
                            <td colspan="5" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-map fs-1 text-muted"></i>
                                    <h5 class="mt-3">未找到区域信息</h5>
                                    <p class="text-muted">请添加新区域或修改搜索条件</p>
                                </div>
                            </td>
                        </tr>
                        {% else %}
                            {% for area in areas %}
                            <tr>
                                <td class="fw-bold">{{ loop.index }}</td>
                                <td>{{ area.AreaName }}</td>
                                <td>{{ area.Latitude or '-' }}</td>
                                <td>{{ area.Longitude or '-' }}</td>
                                <td>
                                    <div class="d-flex gap-2">
                                        <button class="btn btn-sm btn-outline-primary edit-area-btn" 
                                                data-id="{{ area.AreaId }}"
                                                data-name="{{ area.AreaName }}"
                                                data-latitude="{{ area.Latitude }}"
                                                data-longitude="{{ area.Longitude }}">
                                            <i class="bi bi-pencil"></i> 编辑
                                        </button>
                                        <button class="btn btn-sm btn-outline-danger delete-area-btn" 
                                                data-id="{{ area.AreaId }}"
                                                data-name="{{ area.AreaName }}">
                                            <i class="bi bi-trash"></i> 删除
                                        </button>
                                    </div>
                                </td>
                            </tr>
                            {% endfor %}
                        {% endif %}
                    </tbody>
                </table>
            </div>
            
            <!-- 分页控件 -->
            {% if total_pages > 1 %}
            <nav class="px-3 py-3">
                <ul class="pagination justify-content-end mb-0">
                    {% if current_page > 1 %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ current_page-1 }}&search={{ search or '' }}">
                            <i class="bi bi-chevron-left"></i>
                        </a>
                    </li>
                    {% endif %}
                    
                    {% for p in range(1, total_pages+1) %}
                    <li class="page-item {% if p == current_page %}active{% endif %}">
                        <a class="page-link" href="?page={{ p }}&search={{ search or '' }}">{{ p }}</a>
                    </li>
                    {% endfor %}
                    
                    {% if current_page < total_pages %}
                    <li class="page-item">
                        <a class="page-link" href="?page={{ current_page+1 }}&search={{ search or '' }}">
                            <i class="bi bi-chevron-right"></i>
                        </a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>

<!-- 添加区域模态框 -->
<div class="modal fade" id="addAreaModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title"><i class="bi bi-plus-circle me-2"></i>添加新区域</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="addAreaForm" method="post" action="/areas" enctype="application/x-www-form-urlencoded">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">区域名称 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" name="AreaName" required>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">纬度</label>
                            <input type="number" step="0.00000001" class="form-control" name="Latitude">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">经度</label>
                            <input type="number" step="0.00000001" class="form-control" name="Longitude">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">添加区域</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 编辑区域模态框 -->
<div class="modal fade" id="editAreaModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-primary text-white">
                <h5 class="modal-title"><i class="bi bi-pencil me-2"></i>编辑区域</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form id="editAreaForm" method="post" enctype="application/x-www-form-urlencoded">
                <input type="hidden" name="AreaId" id="editAreaId">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">区域名称 <span class="text-danger">*</span></label>
                        <input type="text" class="form-control" name="AreaName" id="editAreaName" required>
                    </div>
                    
                    <div class="row">
                        <div class="col-md-6 mb-3">
                            <label class="form-label">纬度</label>
                            <input type="number" step="0.00000001" class="form-control" name="Latitude" id="editLatitude">
                        </div>
                        <div class="col-md-6 mb-3">
                            <label class="form-label">经度</label>
                            <input type="number" step="0.00000001" class="form-control" name="Longitude" id="editLongitude">
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">保存更改</button>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- 删除确认模态框 -->
<div class="modal fade" id="deleteConfirmModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title"><i class="bi bi-exclamation-triangle me-2"></i>确认删除</h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>确定要删除区域 <strong id="deleteAreaName"></strong> 吗？此操作无法撤销！</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteAreaForm" method="post" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>

{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // 显示操作消息提示
    const urlParams = new URLSearchParams(window.location.search);
    if(urlParams.has('success') || urlParams.has('error')) {
        setTimeout(function() {
            window.scrollTo({ top: 0, behavior: 'smooth' });
        }, 100);
    }
    
    // 添加区域模态框显示时清除表单
    const addModal = document.getElementById('addAreaModal');
    if(addModal) {
        addModal.addEventListener('show.bs.modal', function () {
            const form = this.querySelector('form');
            if(form) form.reset();
        });
    }
    
    // 编辑按钮点击事件
    document.querySelectorAll('.edit-area-btn').forEach(button => {
        button.addEventListener('click', function() {
            const areaId = this.getAttribute('data-id');
            const areaName = this.getAttribute('data-name');
            const latitude = this.getAttribute('data-latitude');
            const longitude = this.getAttribute('data-longitude');
            
            // 填充表单
            document.getElementById('editAreaId').value = areaId;
            document.getElementById('editAreaName').value = areaName;
            document.getElementById('editLatitude').value = latitude;
            document.getElementById('editLongitude').value = longitude;
            
            // 设置表单action
            document.getElementById('editAreaForm').action = `/areas/${areaId}`;
            
            // 显示模态框
            const editModal = new bootstrap.Modal(document.getElementById('editAreaModal'));
            editModal.show();
        });
    });
    
    // 删除按钮点击事件
    document.querySelectorAll('.delete-area-btn').forEach(button => {
        button.addEventListener('click', function() {
            const areaId = this.getAttribute('data-id');
            const areaName = this.getAttribute('data-name');
            
            // 设置删除确认信息
            document.getElementById('deleteAreaName').textContent = areaName;
            document.getElementById('deleteAreaForm').action = `/areas/${areaId}/delete`;
            
            // 显示模态框
            const deleteModal = new bootstrap.Modal(document.getElementById('deleteConfirmModal'));
            deleteModal.show();
        });
    });
    
    // 显示Toast通知
    function showToast(type, message) {
        // 创建Toast元素
        const toastEl = document.createElement('div');
        toastEl.className = `toast align-items-center text-white bg-${type} border-0`;
        toastEl.setAttribute('role', 'alert');
        toastEl.setAttribute('aria-live', 'assertive');
        toastEl.setAttribute('aria-atomic', 'true');
        
        toastEl.innerHTML = `
        <div class="d-flex">
            <div class="toast-body">
                <i class="bi ${type === 'success' ? 'bi-check-circle' : 'bi-exclamation-circle'} me-2"></i>
                ${message}
            </div>
            <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
        </div>
        `;
        
        // 添加到容器
        const toastContainer = document.getElementById('toastContainer');
        if (!toastContainer) {
            // 创建容器如果不存在
            const container = document.createElement('div');
            container.id = 'toastContainer';
            container.className = 'toast-container position-fixed bottom-0 end-0 p-3';
            container.style.zIndex = '9999';
            document.body.appendChild(container);
        }
        
        document.getElementById('toastContainer').appendChild(toastEl);
        
        // 初始化并显示Toast
        const toast = new bootstrap.Toast(toastEl, {
            autohide: true,
            delay: 3000
        });
        toast.show();
    }
});
</script>
{% endblock %}